package com.haixiaoke.smartLock.youdian;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.domain.LockCommandLog;
import com.haixiaoke.domain.LockInfoLog;
import com.haixiaoke.domain.LockUnlockLog;
import com.haixiaoke.domain.LockWarningLog;
import com.haixiaoke.service.impl.LockCommandLogServiceImpl;
import com.haixiaoke.service.impl.LockInfoLogServiceImpl;
import com.haixiaoke.service.impl.LockUnlockLogServiceImpl;
import com.haixiaoke.service.impl.LockWarningLogServiceImpl;
import com.haixiaoke.vo.ydCallBackVo;
import com.haixiaoke.vo.ydDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 优电-事件回调处理
 * 20230625
 * xiaolong
 */
@Service
public class SmartLockYdLogEventHandleService {

    @Autowired
    private LockInfoLogServiceImpl lockInfoLogService;
    @Autowired
    private LockWarningLogServiceImpl lockWarningLogService;
    @Autowired
    private LockCommandLogServiceImpl lockCommandLogService;
    @Autowired
    private LockUnlockLogServiceImpl lockUnlockLogService;

    /**
     * 开锁日志
     *
     * @param vo
     */
    public void logEventHandle(ydCallBackVo vo) {
        LockUnlockLog log = new LockUnlockLog();
        ydDataVo pushContent = vo.getPushContent();
        log.setLockSn(vo.getImei());
        Integer passwordType = pushContent.getPasswordType();
        // 将开锁状态转换为系统开锁类型
        log.setUnlockType(YdUnlockTypeConvert(passwordType));
        log.setUnlockState(0);
        String createTime = vo.getCreateTime();
        log.setUnlockTime(DateUtil.parse(createTime));
        log.setBatteryInfo(String.valueOf(pushContent.getBatteryLevel()));

        Integer deviceStatusType = pushContent.getDeviceStatusType();
        if (deviceStatusType == 0) {
            log.setLockState(LockUnlockLog.LOCK_STATE_ONLINE);
        } else {
            log.setLockState(LockUnlockLog.LOCK_STATE_OFFLINE);
        }
        log.setBrandId(LockWarningLog.YD_BRAND_ID);
        log.setBrandName(LockWarningLog.YD_BRAND_NAME);
        log.setModelId(LockWarningLog.YD_BRAND_MODEL_ID);
        log.setModelName(LockWarningLog.YD_BRAND_MODEL_NAME);
        log.setCreateTime(new Date());
        lockUnlockLogService.insertLockUnlockLog(log);

    }


    /**
     * 心跳日志
     *
     * @param vo
     */
    public void lockOnlineCallBack(ydCallBackVo vo) {
        LockInfoLog log = new LockInfoLog();
        ydDataVo pushContent = vo.getPushContent();
        Integer signalLevel = pushContent.getSignalLevel() * 20;
        log.setSignalStrength(signalLevel);
        log.setBattery(pushContent.getBatteryLevel());
        log.setInfoType(LockInfoLog.INFOTYPE_HEARTBEAT);
        log.setLockSn(vo.getImei());
        log.setCreateTime(new Date());
        log.setBrandId(LockWarningLog.YD_BRAND_ID);
        log.setBrandName(LockWarningLog.YD_BRAND_NAME);
        log.setModelId(LockWarningLog.YD_BRAND_MODEL_ID);
        log.setModelName(LockWarningLog.YD_BRAND_MODEL_NAME);
        lockInfoLogService.insertLockInfoLog(log);
    }


    /**
     * 告警日志
     *
     * @param vo
     */
    public void lockAlarm(ydCallBackVo vo) {
        LockWarningLog log = new LockWarningLog();
        ydDataVo pushContent = vo.getPushContent();
        Integer alarmType = pushContent.getAlarmType();
        // 将开锁状态转换为系统开锁类型
        log.setWarningType(YLTWarningTypeConvert(alarmType));
        log.setLockSn(vo.getImei());
        log.setCreateTime(new Date());
        log.setBrandId(LockWarningLog.YD_BRAND_ID);
        log.setBrandName(LockWarningLog.YD_BRAND_NAME);
        log.setModelId(LockWarningLog.YD_BRAND_MODEL_ID);
        log.setModelName(LockWarningLog.YD_BRAND_MODEL_NAME);
        lockWarningLogService.insertLockWarningLog(log);

    }


    /**
     * 密码同步
     *
     * @param vo
     */
    public void lockPasswordSynchronization(ydCallBackVo vo) {
        ydDataVo pushContent = vo.getPushContent();
        Integer operationType = pushContent.getOperationType();
        if (operationType == 1) {
            LockCommandLog log = new LockCommandLog();
            Integer passwordType = pushContent.getPasswordType();
            // 将开锁状态转换为系统开锁类型
            log.setCommandType(YdPasswordConvert(passwordType));
            log.setLockSn(vo.getImei());

            Integer synchronizationState = pushContent.getSynchronizationState();
            if (synchronizationState == 0) {
                log.setCallbackState(1);
            } else {
                log.setCallbackState(0);
            }
            log.setCreateTime(new Date());
            log.setBrandId(LockWarningLog.YD_BRAND_ID);
            log.setBrandName(LockWarningLog.YD_BRAND_NAME);
            log.setModelId(LockWarningLog.YLT_BRAND_MODEL_ID);
            log.setModelName(LockWarningLog.YD_BRAND_MODEL_NAME);
            lockCommandLogService.insertLockCommandLog(log);
        }
    }

    private Integer YdUnlockTypeConvert(Integer fOpenWay) {

        // 	密码类型（1:指纹；2:密码；3:卡；11:人脸）
        /**
         * 系统开锁类型:1：永久密码 2：指纹 3：临时密码 4：远程开锁 5：蓝牙开锁 6：cookie开锁 7：门卡开锁
         * 8- 撤防/布防 10：机械钥匙开门 11：反锁 12 - 门内开锁 13:装修密码 14:装修密码开锁失败 15:离线开锁
         */
        Integer type = null;
        switch (fOpenWay) {
            case 1:
                type = 2;
                break;
            case 2:
                type = 3;
                break;
            case 3:
                type = 7;
                break;
            case 11:
                type = 16;
                break;
        }
        return type;
    }

    private Integer YdPasswordConvert(Integer fOpenWay) {

        // 	密码类型（1:指纹:2:密码;3:卡;4:临时密码;5:保姆密码;6：SAM公安身份证）
        /**
         * 命令类型: 1:同步临时密码 2：同步成员密码 4：同步门卡 6：同步指纹 7:远程开锁 8:清空指令 9:清空指令(包含蓝牙密码) 10:同步人脸
         */
        Integer type = null;
        switch (fOpenWay) {
            case 1:
                type = 6;
                break;
            case 2:
                type = 1;
                break;
            case 3:
                type = 4;
                break;
            case 4:
                type = 1;
                break;
            case 11:
                type = 10;
                break;
        }
        return type;
    }

    private Integer YLTWarningTypeConvert(Integer warningType) {

        /**
         * 	报警类型：（1：防拆 2：密码连错 3：低电量 5：指纹连错 7：卡连错 8假锁）
         */
        /** '告警类型: 1：撞击报警 2：低电量 3：撬锁报警 5：长时间未关
         * 6：蓝牙cookie错误 8：非法开门报警 9：密码错误自锁报警'*/
        Integer type = null;
        switch (warningType) {
            case 1:
                type = 3;
                break;
            case 2:
                type = 9;
                break;
            case 3:
                type = 2;
                break;
            case 5:
                type = 9;
                break;
            case 7:
                type = 9;
                break;
            case 8:
                type = 10;
                break;
        }
        return type;
    }
}
